package br.com.minhasfinancas.api.model;

import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.validation.constraints.NotNull;

import org.hibernate.annotations.ForeignKey;

import br.com.minhasfinancas.fw.model.AbstractModel;

@Entity
@NamedQueries({
	@NamedQuery(name = "Despesa.findAll", query = "SELECT d FROM Despesa d"),
	@NamedQuery(name = "Despesa.findById", query = "SELECT d FROM Despesa d WHERE d.id = :id"),
	@NamedQuery(name = "Despesa.findByDescricao", query = "SELECT d FROM Despesa d WHERE d.descricao = :descricao"),
	@NamedQuery(name = "Despesa.findByConta", query = "SELECT d FROM Despesa d WHERE d.conta = :conta"),
	@NamedQuery(name = "Despesa.findByTipo", query = "SELECT d FROM Despesa d WHERE d.tipo = :tipo"),
	@NamedQuery(name = "Despesa.findByIdEager", query = "SELECT d FROM Despesa d LEFT JOIN FETCH d.datas WHERE d.id = :id")})
public class Despesa extends AbstractModel<Long> {
	private static final long serialVersionUID = 1L;
	
	public static final String FINDALL = "Despesa.findAll";
	public static final String FINDBYID = "Despesa.findByid";
	public static final String FINDBYDESCRICAO = "Despesa.findByDescricao";
	public static final String FINDBYCONTA = "Despesa.findByConta";
	public static final String FINDBYTIPO = "Despesa.findByTipo";
	public static final String FINDBYIDEAGER = "Despesa.findByIdEager";

	@Id @GeneratedValue(strategy=GenerationType.SEQUENCE)
	@Column(name = "ID_DESPESA")
	private Long id;
	
	@NotNull
	@Column(name = "VALOR")
	private double valor;
	
	@NotNull
	@Column(name = "DESCRICAO", length = 45)
	private String descricao;
	
	@JoinColumn(name = "ID_CONTA")
	@ManyToOne(optional = false)
	@ForeignKey(name = "FK_CONTA_DESPESA")
	private Conta conta;
	
	@JoinColumn(name = "ID_TIPO")
	@ManyToOne(optional = false)
	@ForeignKey(name = "FK_TIPO_DESPESA")
	private Tipo tipo;
	
	@ManyToMany
	@JoinTable(name = "DESPESA_DATA", joinColumns = { @JoinColumn(name = "ID_DESPESA") }, inverseJoinColumns = { @JoinColumn(name = "ID_DATA") })
	private Set<Data> datas;
	
	@Override
	public Long getId() {
		return this.id;
	}
	
	@Override
	public void setId(Long id) {
		this.id = id;
	}

	public double getValor() {
		return valor;
	}

	public void setValor(double valor) {
		this.valor = valor;
	}

	public String getDescricao() {
		return descricao;
	}

	public void setDescricao(String descricao) {
		this.descricao = descricao;
	}

	public Conta getConta() {
		return conta;
	}

	public void setConta(Conta conta) {
		this.conta = conta;
	}

	public Tipo getTipo() {
		return tipo;
	}

	public void setTipo(Tipo tipo) {
		this.tipo = tipo;
	}

	public Set<Data> getDatas() {
		return datas;
	}

	public void setDatas(Set<Data> datas) {
		this.datas = datas;
	}
	
}
